/*
 * @(#)ListAll.java
 * Date 2014-01-20
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package com.chrisuyehara.vista.rpc.procedures.ORWPT;

import java.util.ArrayList;
import java.util.List;

import com.chrisuyehara.vista.rpc.models.PatientIdInfo;

import com.chrisuyehara.vista.rpc.parameters.LiteralParameter;

import com.chrisuyehara.vista.rpc.procedures.AbstractRemoteProcedure;
import com.chrisuyehara.vista.rpc.procedures.Constants;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;


/**
 * Class description
 *
 *
 * @version        v1.0, 2014-01-20
 * @author         Jim Horner
 */
public class ListAll extends AbstractRemoteProcedure {

    /**
     * Constructs ...
     *
     *
     * @param searchString
     * @param start
     */
    public ListAll(String searchString, String start) {

        super("ORWPT LIST ALL");
        addParameter(new LiteralParameter(searchString));
        addParameter(new LiteralParameter(start));
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public PatientIdInfo[] getPatients() {

        List<PatientIdInfo> result = new ArrayList<>();

        String[] lines = StringUtils.split(getResponse(), Constants.LIST_LINE_BREAK);

        if (ArrayUtils.isNotEmpty(lines)) {

            for (String line : lines) {

                String[] fields = StringUtils.split(line, "^");

                if (ArrayUtils.isNotEmpty(fields) && (fields.length > 1)) {

                    PatientIdInfo info = new PatientIdInfo();

                    info.setDfn(fields[0]);
                    info.splitName(fields[1]);

                    result.add(info);
                }
            }
        }

        return result.toArray(new PatientIdInfo[result.size()]);
    }
}
